# Author: Gregory Smith
# Date: 11 - 28 - 2018
# Title: Secret But Constrained Final Script
# Job: Includes the Code For All Models Included in the Main Manuscript
# Note: For all of this code to run, the final_model_output_functions script needs to be run first 

# Standard Error Output Save Folder
# gsmith_2018_io_rr_ses


#---- Packages 

list.of.packages <- c("tidyverse", "cowplot",  "texreg", "devtools", "interplot")

new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

#--- Plots and Data Manipulation 
library(tidyverse)
library(cowplot)

#---- Texreg, Model Output, Interaction Plots 

library(texreg)
library(devtools)
# install_version("clusterSEs", version = "2.5", repos = "http://cran.us.r-project.org")
library(clusterSEs)  # Version 2.5  
library(interplot)  # Version 0.2.1 


#---- Set the Seed 

set.seed(175)

#---- Data 
dta <- read_csv("data/smith_2019_secret_but_constrained_dta.csv")

# glimpse(dta)

#---- Models that use the O'Rourke Sample 

#--- Base Model with divided Predictor and Time Dummies  
divided_orke_base <- glm(orourke_onset ~  divided + 
                           factor(president) - 1 +
                           orourke_peace_years + orourke_peace_years_2  +
                           orourke_peace_years_3,
                         data = dta, family = "binomial")

 divided_orke_base_cse <- cluster.bs.glm(divided_orke_base, dta, ~ ccode_current, boot.reps = 2000)
 clstr_se_clean(divided_orke_base_cse, divided_orke_base, model_name = "divided_orke_base")

 
#---- Full Model (Original)

divided_orke_full <- glm(orourke_onset ~  divided + 
                           election  + approval_q1 +
                           democracy + cinc_proportion + clean_russ_inf + 
                            oversight_change + 
                           orourke_peace_years + orourke_peace_years_2  + orourke_peace_years_3,
                         data = dta, family = "binomial")

divided_orke_cse <- cluster.bs.glm(divided_orke_full, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(divided_orke_cse, divided_orke_full, model_name = "divided_orke_full")

#----- Interaction between Divided Government and Oversight Change (No fixed effects)

orke_divided_interaction <- glm(orourke_onset ~  divided + oversight_change +
                                  election  + approval_q1 + 
                                  democracy + cinc_proportion +
                                  clean_russ_inf + oversight_change +
                                  oversight_divided_interact +
                                  orourke_peace_years + orourke_peace_years_2  +
                                  orourke_peace_years_3,
                                data = dta, family = "binomial")

orke_divided_interaction_cse <- cluster.bs.glm(orke_divided_interaction, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(orke_divided_interaction_cse, orke_divided_interaction, model_name = "orke_divided_interaction")

#---- Models that use the Pooled (O'Rourke + Berger) Sample 

# Base Model with divided Predictor and Time Dummies  
divided_cmb_base <- glm(us_covert_onset ~  divided +
                          factor(president) - 1 +
                          us_peace_years + us_peace_years_2 + us_peace_years_3,
                        data = dta, family = "binomial")

divided_cmb_base_cse <- cluster.bs.glm(divided_cmb_base, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(divided_cmb_base_cse, divided_cmb_base, model_name = "divided_cmb_base")

#---- Full Model
divided_cmb_full <- glm(us_covert_onset ~  divided + 
                          election  + approval_q1 + democracy + cinc_proportion +
                          clean_russ_inf + oversight_change +
                          us_peace_years + us_peace_years_2 + us_peace_years_3, 
                        data = dta, family = "binomial")

divided_cmb_full_cse <- cluster.bs.glm(divided_cmb_full, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(divided_cmb_full_cse, divided_cmb_full, model_name = "divided_cmb_full")

#----- Interaction between divided government and oversight change (No fixed effects)

cmb_divided_interaction <- glm(us_covert_onset ~  divided + oversight_change +
                                 election  + approval_q1 + democracy +
                                 cinc_proportion +
                                 clean_russ_inf + oversight_change +
                                 oversight_divided_interact +
                                 us_peace_years + us_peace_years_2 +
                                 us_peace_years_3,
                               data = dta, family = "binomial") 

cmb_divided_interaction_cse <- cluster.bs.glm(cmb_divided_interaction, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(cmb_divided_interaction_cse, cmb_divided_interaction, model_name = "cmb_divided_interaction")


#----- Minor and Major US Initiated GML MIDs 

#----- Minor MIDs

minor_us_base_mid_div <- glm(minor_us_onset ~  divided + 
                               factor(president) - 1 + 
                               minor_us_py + I(minor_us_py^2)  +
                               I(minor_us_py^3), 
                             data = dta, family = "binomial")

minor_us_base_mid_div_cse <- cluster.bs.glm(minor_us_base_mid_div, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(minor_us_base_mid_div_cse, minor_us_base_mid_div, model_name = "minor_us_base_mid_div")

minor_us_mid_div <- glm(minor_us_onset ~  divided + 
                          election  + approval_q1 + 
                          democracy + cinc_proportion + clean_russ_inf + 
                          oversight_change + 
                          factor(president) - 1 + 
                          minor_us_py + I(minor_us_py^2)  +
                          I(minor_us_py^3), 
                        data = dta, family = "binomial")

minor_us_mid_div_cse <- cluster.bs.glm(minor_us_mid_div, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(minor_us_mid_div_cse, minor_us_mid_div, model_name = "minor_us_mid_div")

#----- Major MIDs 

major_us_base_mid_div <- glm(major_us_onset ~  divided + 
                               factor(president) - 1 + 
                               major_us_py + I(major_us_py^2)  +
                               I(major_us_py^3), 
                             data = dta, family = "binomial")

major_us_base_mid_div_cse <- cluster.bs.glm(major_us_base_mid_div, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(major_us_base_mid_div_cse, major_us_base_mid_div, model_name = "major_us_base_mid_div")


major_us_mid_div <- glm(major_us_onset ~ divided + 
                          election  + approval_q1 +
                          democracy + cinc_proportion + clean_russ_inf + 
                          factor(president) - 1 + 
                          major_us_py + I(major_us_py^2)  +
                          I(major_us_py^3), 
                        data = dta, family = "binomial")

major_us_mid_div_cse <- cluster.bs.glm(major_us_mid_div, dta, ~ ccode_current, boot.reps = 2000)
clstr_se_clean(major_us_mid_div_cse, major_us_mid_div, model_name = "major_us_mid_div")


#----- Interaction Plots 
# Note: Interplot needs the interaction to be specified directly in the formula

oversight_label <- c("1947 - 1974", "1975 - 1989")

orourke_interact <- glm(orourke_onset ~  divided + 
                          election  + approval_q1 +
                          democracy + cinc_proportion + clean_russ_inf + 
                          oversight_change + 
                          divided*oversight_change +
                           orourke_peace_years + orourke_peace_years_2  +
                           orourke_peace_years_3,
                         data = dta, family = "binomial")

pooled_interact <- glm(us_covert_onset ~  divided +
                         election  + approval_q1 +
                         democracy + cinc_proportion + clean_russ_inf + 
                         oversight_change + 
                         divided*oversight_change +
                         us_peace_years + us_peace_years_2 + us_peace_years_3,
                       data = dta, family = "binomial")

int.1 <- interplot(m = orourke_interact, var1 = "divided", var2 = "oversight_change",
                   point = TRUE, hist = FALSE) +
  scale_x_continuous(breaks = 0:1, labels = oversight_label) +
  xlab("Oversight Period") +
  ylab("Estimated Coefficient of Divided Government") +
  theme_bw() +
  labs(subtitle = "O'Rourke Sample Without Presdiential Fixed Effects",
       caption = "") +
  theme(plot.title = element_text(face = "bold")) + 
  geom_hline(yintercept = 0, linetype = "dashed")

int.2 <- interplot(m = pooled_interact, var1 = "divided", var2 = "oversight_change",
                   point = TRUE, hist = FALSE) +
  scale_x_continuous(breaks = 0:1, labels = oversight_label) +
  xlab("Oversight Period") +
  ylab("") + 
  labs(subtitle = "Pooled Sample Without Presdiential Fixed Effects",
       caption = "") +
  theme(plot.title = element_text(face="bold")) + theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")


#----- Puts the interaction plots into grids 

interact_grid <- plot_grid(int.1, int.2,
                           rel_widths = c(.5, .5), 
                           rel_heights = c(.5, .5))

#--- Saves the plots 
# save_plot("~/Desktop/oversight_interaction.pdf", interact_grid, base_width = 13)

